<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: bootstrap-editable-css"/>
    <th:block th:include="include :: header('新建出库单')"/>
    <th:block th:include="include :: layout-latest-css"/>
    <th:block th:include="include :: ztree-css"/>
    <!--<style>-->
    <!--table tr td:nth-child(2){-->
    <!--display: none;-->
    <!--}-->
    <!--table th:nth-child(2){-->
    <!--display: none;-->
    <!--}-->
    <!--</style>-->
</head>

<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label class="is-required">调拨类型：</label>
                            <select name="bwart" id="bwart" required>
                                <option value="411">411调拨销售订单库存到非限制</option>
                                <option value="413">413调拨非限制到销售订单库存</option>
                                <option value="413">413E调拨销售订单库存到销售订单库存</option>
                            </select>
                        </li>
                        <li>
                            <label>参考凭证编号：</label>
                            <input type="text" id="refDocNo" name="refDocNo"/>
                        </li>
                        <li>
                            <label>凭证抬头文本：</label>
                            <input type="text" id="headerTxt" name="headerTxt"/>
                        </li>
                        <li>
                            <label>过账日期：</label>
                            <input type="text" id="budat" name="budat"/>
                        </li>

                        <li>
                            <a id="save" class="btn btn-primary btn-rounded btn-sm" onclick="saveTransfer()"><i
                                    class="fa fa-search"></i>&nbsp;保存单据</a>
                            <a id="reset" class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                            <!--</li>-->
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a id="selectProduct" class="btn btn-success" onclick="selectProduct()">
                <i class="fa fa-plus"></i> 选择物资
            </a>
            <a id="continue" class="btn btn-success" onclick="newReceiptAgain()">
                <i class="fa fa-plus"></i> 继续建单
            </a>

        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-table-editable-js"/>
<th:block th:include="include :: layout-latest-js"/>
<th:block th:include="include :: ztree-js"/>
<script th:inline="javascript">

    var prefix = ctx + "sap/StockTransfer";

    $(function () {
        var options = {
            // url: "/out/stockoutview/list",
            createUrl: prefix + "/addSave1",
            clickToSelect: true,
            height: $(window).height() / 1.5,
            contenteditable: true,
            // updateUrl: prefix + "/edit/{id}",
            // removeUrl: prefix + "/remove",
            // exportUrl: prefix + "/export",
            // saveUrl:"/out/stockout/addSave",
            modalName: "出库主单",
            editable: true,
            columns: [
                {
                    checkbox: true

                },
                {
                    field: 'onlyIdentity',
                    title: 'ID',
                    visible: false

                },
                {
                    field: 'werks',
                    title: '工厂',
                    visible: true,
                    editable: {
                        type: 'text',
                        disabled: false // 是否禁用编辑
                    }
                },
                {
                    field: 'werksTo',
                    title: '目标工厂',
                    visible: true,
                    editable: {
                        type: 'text',
                        disabled: false // 是否禁用编辑
                    }
                },
                {
                    field: 'matnr',
                    title: '物料号',
                    visible: true,
                    editable: {
                        type: 'text',
                        disabled: false // 是否禁用编辑
                    }
                },
                {
                    field: 'matnrTo',
                    title: '目标物料号',
                    visible: true,
                    editable: {
                        type: 'text',
                        disabled: false // 是否禁用编辑
                    }
                },
                {
                    field: 'lgort',
                    title: '源库存地点',
                    visible: true,
                    editable: {
                        type: 'text',
                        disabled: false // 是否禁用编辑
                    }
                },
                {
                    field: 'lgortTo',
                    title: '目标库存地点',
                    visible: true,
                    editable: {
                        type: 'text',
                        disabled: false // 是否禁用编辑
                    }
                },
                {
                    field: 'erfmg',
                    title: '数量',
                    visible: true,
                    editable: {
                        type: 'text',
                        title: '出库数量',
                        disabled: false, // 是否禁用编辑
                        validate: function (value) {
                            if (isNaN(value)) {
                                return '请填写数字';
                            }
                            if (value.length == 0) {
                                return '数量不能为空';
                            }
                        }

                    },
                    formatter: function (value, row, index) {
                        if (value) {
                            return value;
                        } else {
                            row.erfmg = '';
                            return '请填写出库数量';
                        }
                    }
                },
                {
                    field: 'erfme',
                    title: '单位'
                },
                {
                    field: 'charg',
                    title: '批号',
                    visible: true,
                    editable: {
                        type: 'text',
                        disabled: false // 是否禁用编辑
                    }
                },
                {
                    field: 'chargTo',
                    title: '目标批号',
                    visible: true,
                    editable: {
                        type: 'text',
                        disabled: false // 是否禁用编辑
                    }
                },
                {
                    field: 'aufnr',
                    title: '工单',
                    visible: true,
                    editable: {
                        type: 'text',
                        disabled: false // 是否禁用编辑
                    }
                },
                {
                    field: 'rsnum',
                    title: '预留',
                    visible: true,
                    editable: {
                        type: 'text',
                        disabled: false // 是否禁用编辑
                    }
                },
                {
                    field: 'rspos',
                    title: '预留行号',
                    visible: true,
                    editable: {
                        type: 'text',
                        disabled: false // 是否禁用编辑
                    }
                },
                {
                    field: 'kdauf',
                    title: '源销售订单号',
                    visible: true,
                    editable: {
                        type: 'text',
                        disabled: false // 是否禁用编辑
                    }
                },
                {
                    field: 'kdpos',
                    title: '源销售订单行号',
                    visible: true,
                    editable: {
                        type: 'text',
                        disabled: false // 是否禁用编辑
                    }
                },
                {
                    field: 'matKdauf',
                    title: '目标销售订单号',
                    visible: true,
                    editable: {
                        type: 'text',
                        disabled: false // 是否禁用编辑
                    }
                },
                {
                    field: 'matKdpos',
                    title: '目标销售订单行号',
                    visible: true,
                    editable: {
                        type: 'text',
                        disabled: false // 是否禁用编辑
                    }
                }
            ]
        };
        $.table.init(options);
        createTime();

    });

    //日期框
    function createTime() {
        var _input = document.getElementById('budat');

        var date = new Date();
        var seperator = "";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentDate = year + seperator + month + seperator + strDate; //当前日期

        _input.value = currentDate; //赋值给input.value
    }

    //选择物资
    function selectProduct() {
        var outType = $("#outType").val();
        if (outType == '') {
            $.modal.alertWarning("请先选择单据类型");
            return
        }

        var url = "/sap/StockTransfer/selectProduct";
        $.modal.open("选择物品信息", url);
    }

    //关闭物资框
    function closeSelectProduct(row) {

        if (row) {
            var rows2 = $('#bootstrap-table').bootstrapTable('getData');

            var zTreeDoorData = [];
            // var doorIdArr = data.doorIdArr;
            //点击每个节点重新获取数据的初始化操作
            // $('#bootstrap-table').bootstrapTable('removeAll');
            zTreeDoorData.splice(0, zTreeDoorData.length);

            for (var i = 0; i < row.length; i++) {


                treedoor = {
                    werks: '2240',
                    werksTo: '2240',
                    matnr: row[i].productId,
                    matnrTo: row[i].productId,
                    erfme: row[i].unit
                };
                zTreeDoorData.push(treedoor);
            }
            for (var j = 0; j < zTreeDoorData.length; j++) {
                var dataTree2 = zTreeDoorData[j];
                $('#bootstrap-table').bootstrapTable('append', dataTree2);
            }
        }
    }

    //保存单据
    function saveTransfer() {

        var rows = $('#bootstrap-table').bootstrapTable('getData');
        if (rows.length == 0) {
            $.modal.alertWarning("请选择物资");
            return;
        }

        if ($.validate.form()) {
            var params = new Object();
            params.zflWmsStockTransferHeader = $("#formId").serializeJson();
            params.zflWmsStockTransferItemList = $("#bootstrap-table").bootstrapTable("getData", false);

            var opt = new Object()

            opt.url = '/sap/StockTransfer/addSave1';
            opt.data = JSON.stringify(params);
            ajaxRequestJsonBody(opt, onSaveSuccess);

            function ajaxRequestJsonBody(opt, callback) {

                $.ajax({
                    url: opt.url,
                    /**必须是POST方法**/
                    type: 'post',
                    /**必须制定请求的类型**/
                    contentType: 'application/json',
                    data: opt.data,
                    dataType: 'json',
                    success: function (result) {
                        if (result.code == web_status.SUCCESS) {
                            modal.alertSuccess(result.msg);
                        } else if (result.code == web_status.WARNING) {
                            modal.alertWarning(result.msg);
                        } else {
                            modal.alertError(result.msg);
                        }
                        if (callback)
                            callback(result);
                    },
                    error: function (XMLHttpRequest, textStatus, error) {
                        DataTable.config.ajaxError(XMLHttpRequest, textStatus, error, "设置失败");
                    }
                });
            }

        }
        modal = {
            // 弹出提示
            alert: function (content, type) {
                // $.messager.show({
                //     icon: type,
                //     title: "系统提示",
                //     msg: content,
                //     timeout:2000
                // });
            },
            // 错误提示
            alertError: function (content) {
                modal.alert(content, modal_status.FAIL);
            },
            // 成功提示
            alertSuccess: function (content) {
                modal.alert(content, modal_status.SUCCESS);
            },
            // 警告提示
            alertWarning: function (content) {
                modal.alert(content, modal_status.WARNING);
            },
            // 重新加载
            reload: function () {
                parent.location.reload();
            }
        }
    }

    //保存单据后的回调函数
    function onSaveSuccess(data) {
        if (data.code == web_status.SUCCESS) {
            $('#stockoutId').attr("value", data.data);
            $('#save').addClass('disabled'); // Disables visually
            $('#selectProduct').addClass('disabled'); // Disables visually
            $('#reset').addClass('disabled');
            $('#delete').addClass('disabled');

        }
    }

    //继续建单
    function newReceiptAgain() {
        window.location.reload();
        $.form.reset()
    }

</script>
</body>
</html>